package com.smart.pay.ali.service.impl;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.smart.pay.api.AliPayService;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * 支付方式
 * 1  web应用支付
 * 2  移动端支付(只需要生成支付链接)
 * 3  移动端web支付
 */
@DubboService
public class AliPayServiceImpl implements AliPayService {

    public String appPay(String orderSn) throws Exception {
        // 远程调用订单服务
        try {
            AlipayTradeAppPayResponse response = Factory.Payment.App().pay("QF-" + orderSn, orderSn, "1.00");
            if (ResponseChecker.success(response)) {
                return response.body;
            } else {
                throw new Exception("生成支付链接失败");
            }
        } catch (Exception ex) {
            throw new Exception("生成支付链接失败");
        }
    }

    public String webPay() throws Exception {
        AlipayTradePagePayResponse pay = Factory.Payment.Page().pay("qf-商城支付", "1234", "1.00", "https://www.baidu.com");
        return pay.body;
    }

    @Override
    public String pay(int type, String orderSN) throws Exception {
        String payLink = null;
        if (type == 1) {
            payLink = appPay(orderSN);
        }
        return payLink;
    }

}
